package com.tsd.system.service.impl;

import com.tsd.CommonMsgNameConstants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysDataObjAccessRuleMapper;
import com.tsd.system.entity.SysDataObjAccessRule;
import com.tsd.system.entity.SysDataObjAccessRuleExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysDataObjAccessRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysDataObjAccessRule 服务实现层
 *
 * @author david
 * @date 2021/07/08 15:49:18
 */
@Service
public class SysDataObjAccessRuleServiceImpl extends BaseServiceImpl implements SysDataObjAccessRuleService {

    @Resource
    private SysDataObjAccessRuleMapper sysDataObjAccessRuleMapper;

    @Override
    public SysDataObjAccessRule getSysDataObjAccessRuleBySid(String sid) throws Exception {
        return sysDataObjAccessRuleMapper.selectBySid(sid);
    }

    @Override
    public SysDataObjAccessRuleExt queryBySid(String sid) throws Exception {
        return sysDataObjAccessRuleMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysDataObjAccessRule(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            SysDataObjAccessRule old = sysDataObjAccessRuleMapper.selectBySid(sid);
            super.checkEmpty(old, CommonMsgNameConstants.ERROR_MSG_RECORD_NOT_EXISTS);
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            sysDataObjAccessRuleMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveSysDataObjAccessRule(SysDataObjAccessRule record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getSid())) {
            super.checkEmpty(record.getCode(), "请输入编码");
            super.checkEmpty(record.getName(), "请输入名称");
            super.autoInjectBaseData(record, opUser, TYPE_CREATE);
            sysDataObjAccessRuleMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, TYPE_UPDATE);
            sysDataObjAccessRuleMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<SysDataObjAccessRuleExt> findSysDataObjAccessRules(Map<String, Object> params, PageBean page) throws Exception {
        return sysDataObjAccessRuleMapper.selectByPage(params, page);
    }

    @Override
    public List<SysDataObjAccessRuleExt> findSysDataObjAccessRules(Map<String, Object> params) throws Exception {
        return sysDataObjAccessRuleMapper.selectByParams(params);
    }
}
